window.ApiSignutils = {
    /**
     * ============= 参数配置 =================
     */
    /**
     * 接口密钥.
     */
    __pwd: '5bRWj8WJ',
    /**
     * 签名Hash算法类型：MD5、SHA1.
     */
    __hash: 'MD5',
    /**
     * 认证请求头Key名称.
     */
    __keyName: 'app-authorize',
    /**
     * ============= 参数配置 =================
     */

    /**
     * 生成随机子符串.
     * @param len 长度.
     * @returns {string}
     * @private
     */
    __getRandomString: function (len) {
        len = len || 32;
        // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
        var char = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
        var maxPos = char.length;
        var str = '';
        for (var i = 0; i < len; i++) {
            str += char.charAt(Math.floor(Math.random() * maxPos));
        }
        return str;
    },

    /**
     * 创建nonce.
     * @returns {string}
     * @private
     */
    __createNonce: function () {
        var date = new Date();
        var nonce = parseInt(date.getTime()/1000).toString() + ":" + this.__getRandomString(6);
        return nonce;
    },

    /**
     * 创建请求签名.
     * 注：需要引入CryptoJS库.
     * @param url 请求地址.
     */
    createNonce: function (url) {
        var nonce = this.__createNonce();
        var content = url + "\n" + nonce + "\n" + this.__pwd;
        var sign = '';
        if ('MD5' === this.__hash) {
            sign = CryptoJS.MD5(content)
        } else if ('SHA1' === this.__hash) {
            sign = CryptoJS.SHA1(content)
        } else {
            throw new Error("不支持的Hash算法")
        }

        var header = {};
        header[this.__keyName] = "nonce=" + nonce + ";sign="+sign
        return header;
    }
};
